package cn.qdu.dao;

import cn.qdu.model.Like;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class LikeDao {
    public boolean addLike(Like like) throws SQLException {
        String sql = "INSERT INTO likes(post_id, user_id, type) VALUES(?, ?, ?)";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, like.getPostId());
            stmt.setString(2, like.getUserId());
            stmt.setString(3, like.getType());

            return stmt.executeUpdate() > 0;
        }
    }


    public int getLikeCountByPostId(int postId, String type) throws SQLException {
        String sql = "SELECT COUNT(*) FROM likes WHERE post_id = ? AND type = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, postId);
            stmt.setString(2, type);

            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1);
                }
            }
        }
        return 0;
    }

    public List<Like> getLikesByPostId(int postId) throws SQLException {
        List<Like> likes = new ArrayList<>();
        String sql = "SELECT l.*, u.nickname, u.avatar FROM likes l " +
                "JOIN users u ON l.user_id = u.user_id " +
                "WHERE l.post_id = ? ORDER BY l.created_at DESC";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, postId);

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Like like = new Like();
                    like.setLikeId(rs.getInt("like_id"));
                    like.setPostId(rs.getInt("post_id"));
                    like.setUserId(rs.getString("user_id"));
                    like.setType(rs.getString("type"));
                    like.setCreatedAt(rs.getTimestamp("created_at"));
                    like.setUserNickname(rs.getString("nickname"));
                    like.setUserAvatar(rs.getString("avatar"));

                    likes.add(like);
                }
            }
        }
        return likes;
    }
    // 检查用户是否对某帖子有特定类型的点赞
    public boolean checkLikeExists(int postId, String userId, String type) throws SQLException {
        String sql = "SELECT COUNT(*) FROM likes WHERE post_id = ? AND user_id = ? AND type = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, postId);
            stmt.setString(2, userId);
            stmt.setString(3, type);

            try (ResultSet rs = stmt.executeQuery()) {
                return rs.next() && rs.getInt(1) > 0;
            }
        }
    }

    // 删除点赞记录
    public boolean deleteLike(int postId, String userId) throws SQLException {
        String sql = "DELETE FROM likes WHERE post_id = ? AND user_id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, postId);
            stmt.setString(2, userId);

            return stmt.executeUpdate() > 0;
        }
    }
}